home *** CD-ROM | disk | FTP | other *** search
- /*
- * globalfeats.c
- *
- * Practical Algorithms for Image Analysis
- *
- * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
- */
-
- /* GLOBALFEATS: program determines global features of image
- * usage: globalfeats inimg [-L]
- *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include "tiffimage.h" /* picfile info on images */
- #include "images.h"
- extern void print_sos_lic ();
-
- #define OFF 0
- #define ON 255
-
- long usage (short);
- long input (int, char **);
-
- main (argc, argv)
- int argc;
- char *argv[];
- {
- Image *imgI; /* I/O image structure */
- unsigned char **image; /* input/output image */
- long height, width; /* size of I/O images */
- long nPix; /* no. pixels total (minus 1-pix borders) */
- long nOn; /* no. of ON-valued pixels */
- double sumXOn, sumYOn; /* sum of x,y of ON-pixels */
- double sumXOnSq, sumYOnSq; /* sum of sqrs. of x,y of ON-pixels */
- long sumEdge; /* number of edge pixels */
- double avgX, avgY; /* average x,y locations */
- double stdDevX, stdDevY; /* std. deviation of x,y locations */
-
- long value;
- long x, y;
-
- /* user input */
- if (input (argc, argv) < 0)
- return (-1);
-
- imgI = ImageIn (argv[1]);
- image = imgI->img;
- height = ImageGetHeight (imgI);
- width = ImageGetWidth (imgI);
-
- /* initialize */
- nPix = nOn = 0;
- sumXOn = sumYOn = 0;
- sumXOnSq = sumYOnSq = 0;
- sumEdge = 0;
-
- /* determine global features */
- for (y = 1; y < height - 1; y++) {
- for (x = 1; x < width - 1; x++) {
- value = (long) image[y][x];
- nPix++;
- if (value != OFF) {
- nOn++;
- sumXOn += x;
- sumYOn += y;
- sumXOnSq += x * x;
- sumYOnSq += y * y;
- if (image[y - 1][x] == OFF || image[y - 1][x + 1] == OFF
- || image[y][x + 1] == OFF || image[y + 1][x + 1] == OFF
- || image[y + 1][x] == OFF || image[y + 1][x - 1] == OFF
- || image[y][x - 1] == OFF || image[y - 1][x - 1] == OFF)
- sumEdge++;
- }
- }
- }
-
- avgX = (double) sumXOn / (double) nOn;
- avgY = (double) sumYOn / (double) nOn;
- stdDevX = sqrt (sumXOnSq / nOn - avgX * avgX);
- stdDevY = sqrt (sumYOnSq / nOn - avgY * avgY);
-
- /* list results of global feature determination */
- printf ("Image size: %dx%d\n", width, height);
- printf ("Image size within 1-pixel border: %dx%d\n",
- width - 2, height - 2);
- printf ("Number of Pixels: %d\n", nPix);
- printf ("Number of ON-pixels: %d, (%5.1f%%)\n", nOn,
- (double) nOn / (double) nPix * 100.0);
- printf ("1st Moment: (%5.2f, %5.2f)\n", avgX, avgY);
- printf ("2nd Moment: (%5.2f, %5.2f)\n", stdDevX, stdDevY);
- printf ("Number of Edge Pixels: %d, (%5.1f%%)\n",
- sumEdge, (double) sumEdge / (double) nPix * 100.0);
-
- return (0);
- }
-
-
-
- /* USAGE: function gives instructions on usage of program
- * usage: usage (flag)
- * When flag is 1, the long message is given, 0 gives short.
- */
-
- long
- usage (flag)
- short flag; /* flag =1 for long message; =0 for short message */
- {
-
- /* print short usage message or long */
- printf ("USAGE: globalfeats inimg [-L]\n");
- if (flag == 0)
- return (-1);
-
- printf ("\nglobalfeats determines some global features\n");
- printf ("of input image and prints out these results to stdout;\n");
- printf ("global features determined are:\n");
- printf (" -- image size\n");
- printf (" -- total number of pixels\n");
- printf (" -- number of ON-valued pixels (where ON is black intensity)\n");
- printf (" -- percentage of ON-valued pixels to total pixels\n");
- printf (" -- 1st moment or average x,y coordinate location of ON-pixels\n");
- printf (" -- 2nd moment of x,y locations of ON-pixels\n");
- printf (" -- number of edge pixels\n");
- printf (" -- percentage of edge pixels to total pixels.\n");
- printf (" NOTE: that the calculations are performed on all pixels\n");
- printf (" EXCEPT the 1-pixel wide borders of pixels at the sides\n");
- printf (" of the image.\n\n");
- printf ("ARGUMENTS:\n");
- printf (" inimg: input image filename (TIF)\n\n");
- printf ("OPTIONS:\n");
- printf (" -L: print Software License for this module\n");
-
- return (-1);
- }
-
-
- /* INPUT: function reads input parameters
- * usage: input (argc, argv)
- */
-
- #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
-
- long
- input (argc, argv)
- int argc;
- char *argv[];
- {
-
- if (argc == 1)
- USAGE_EXIT (1);
- if (argc >= 3 && strcmp (argv[2], "-L") == 0) {
- print_sos_lic ();
- exit (0);
- }
-
- return (0);
- }
-